/*
 * 文件名：BpastDynamicNormServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月17日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.bpast.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.bpa.service.BizcostApplyDetailService;
import top.infopub.job.bpa.service.BizcostApplyService;
import top.infopub.job.bpa.service.ContractExtService;
import top.infopub.job.bpa.service.EffectSureService;
import top.infopub.job.bpa.service.IncaeSureService;
import top.infopub.job.bpa.service.InnerAgrtService;
import top.infopub.job.bpa.service.InnerSettleService;
import top.infopub.job.bpa.service.OutcaeSureService;
import top.infopub.job.bpa.service.ProContractService;
import top.infopub.job.bpa.service.ProSettleService;
import top.infopub.job.bpa.service.SubpackCotService;
import top.infopub.job.bpast.service.BpastDynamicNormService;
import top.infopub.job.bpast.service.DynamicNormService;
import top.infopub.job.fia.service.PayBillDetailService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.bpast.DynamicNorm;
import top.infopub.model.hr.OrgInfo;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


@Service
public class BpastDynamicNormServiceImpl implements BpastDynamicNormService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private EffectSureService effectSureService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private InnerSettleService innerSettleService;

    @Autowired
    private ProSettleService proSettleService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private SubpackCotService subpackCotService;

    @Autowired
    private InnerAgrtService innerAgrtService;

    @Autowired
    private ContractExtService contractExtService;

    @Autowired
    private BizcostApplyService bizcostApplyService;

    @Autowired
    private PayBillDetailService payBillDetailService;

    @Autowired
    private BizcostApplyDetailService bizcostApplyDetailService;

    @Autowired
    private DynamicNormService dynamicNormService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void bpastDynamicNormJob() {
        //查询所有项目合同名称
        Map<String, Object> proContractWhere = Maps.newHashMap();
        proContractWhere.put("processStatus", "9");
        List<ProContract> proContracts = proContractService.selectByMapWhere(proContractWhere);
        for (ProContract proContract : proContracts) {
            DynamicNorm dynamicNorm = new DynamicNorm();
            //往表里存放项目信息
            dynamicNorm.setProCode(proContract.getProCode());
            dynamicNorm.setProName(proContract.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(proContract.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                dynamicNorm.setOrgProName(orgInfos.get(0).getCompName());
                dynamicNorm.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    dynamicNorm.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    dynamicNorm.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            dynamicNorm.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            dynamicNorm.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            dynamicNorm.setStsMonth(ParserUtil.prevMonth());// 上个月
            //所处阶段
            //1.查创效确认中是否存在
            int countEffectSure = effectSureService.count(proContract.getProCode());
            if (countEffectSure > 0) {
                dynamicNorm.setStage(9);
            }
            else {
                //2.查对内结算中是否存在
                int countInnerSettle = innerSettleService.count(proContract.getProCode());
                if (countInnerSettle > 0) {
                    dynamicNorm.setStage(8);
                }
                else {
                    //3.查项目结算中是否存在
                    int countProSettle = proSettleService.count(proContract.getProCode());
                    if (countProSettle > 0) {
                        dynamicNorm.setStage(7);
                    }
                    else {
                        //4.查对内产值确权中是否存在
                        int countIncaeSure = incaeSureService.count(proContract.getProCode());
                        if (countIncaeSure > 0) {
                            dynamicNorm.setStage(6);
                        }
                        else {
                            //5.查对外产值确权中是否存在
                            int countOutcaeSure = outcaeSureService.count(proContract.getProCode());
                            if (countOutcaeSure > 0) {
                                dynamicNorm.setStage(5);
                            }
                            else {
                                //6.查分包合同中是否存在
                                int countSubpackCot = subpackCotService.count(proContract.getProCode());
                                if (countSubpackCot > 0) {
                                    dynamicNorm.setStage(4);
                                }
                                else {
                                    //7.查内部协议中是否存在
                                    int countInnerAgrt = innerAgrtService.count(proContract.getProCode());
                                    if (countInnerAgrt > 0) {
                                        dynamicNorm.setStage(3);
                                    }
                                    else {
                                        //8.查承继协议中是否存在
                                        int countContractExt = contractExtService.count(proContract.getProCode());
                                        if (countContractExt > 0) {
                                            dynamicNorm.setStage(2);
                                        }
                                        else {
                                            //9.查项目合同中是否存在
                                            dynamicNorm.setStage(1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //创效金额(元)
            Map<String, Object> effectFeeWhere = Maps.newHashMap();
            effectFeeWhere.put("stsMonth", dynamicNorm.getStsMonth());
            effectFeeWhere.put("hrOrgProId", dynamicNorm.getHrOrgProId());
            BigDecimal effectFee = effectSureService.selectTypeOfEffectFee(effectFeeWhere);
            dynamicNorm.setEffectAmount(effectFee);
            //对外确权金额(下浮前)(元)
            Map<String, Object> outSureWhereBefore = Maps.newHashMap();
            outSureWhereBefore.put("sureMonth", dynamicNorm.getStsMonth());
            outSureWhereBefore.put("proCode", dynamicNorm.getProCode());
            BigDecimal proQaOfOutSureBofore = outcaeSureService.selectProQaOfOutSureBefore(outSureWhereBefore);
            dynamicNorm.setOutCaePre(proQaOfOutSureBofore);
            //对外确权金额(下浮后)(元)
            Map<String, Object> outSureWhere = Maps.newHashMap();
            outSureWhere.put("sureMonth", dynamicNorm.getStsMonth());
            outSureWhere.put("proCode", dynamicNorm.getProCode());
            BigDecimal proQaOfOutSure = outcaeSureService.selectProQaOfOutSure(outSureWhere);
            dynamicNorm.setOutCaeAfter(proQaOfOutSure);
            //对内确权金额(下浮前)(元)
            Map<String, Object> innerSureWhereBefore = Maps.newHashMap();
            innerSureWhereBefore.put("sureMonth", dynamicNorm.getStsMonth());
            innerSureWhereBefore.put("proCode", dynamicNorm.getProCode());
            BigDecimal proQaOfInnerSureBofore = incaeSureService.selectProQaOfInnerSureBefore(outSureWhereBefore);
            dynamicNorm.setInCaePre(proQaOfInnerSureBofore);
            //对内确权金额(下浮后)(元)
            Map<String, Object> innerSureWhere = Maps.newHashMap();
            innerSureWhere.put("sureMonth", dynamicNorm.getStsMonth());
            innerSureWhere.put("proCode", dynamicNorm.getProCode());
            BigDecimal proQaOfInnerSure = incaeSureService.selectProQaOfInnerSure(innerSureWhere);
            dynamicNorm.setInCaeAfter(proQaOfInnerSure);
            //建安毛利(元)
            Map<String, Object> grossProfitWhere = Maps.newHashMap();
            grossProfitWhere.put("sureMonth", dynamicNorm.getStsMonth());
            grossProfitWhere.put("proCode", dynamicNorm.getProCode());
            BigDecimal grossProfit = outcaeSureService.selectProQaOfGrossProfit(grossProfitWhere);
            dynamicNorm.setGrossCaeProfit(grossProfit);
            //工程结算价(元)
            Map<String, Object> applySettProAmount = Maps.newHashMap();
            applySettProAmount.put("sureMonth", dynamicNorm.getStsMonth());
            applySettProAmount.put("proCode", dynamicNorm.getProCode());
            BigDecimal settProAmount = bizcostApplyDetailService.selectApplySettProAmount(applySettProAmount);
            dynamicNorm.setProSettPrice(settProAmount);
            //结算应付金额(元)
            Map<String, Object> applySettShouldAmount = Maps.newHashMap();
            applySettShouldAmount.put("sureMonth", dynamicNorm.getStsMonth());
            applySettShouldAmount.put("proCode", dynamicNorm.getProCode());
            BigDecimal settShouldAmount = bizcostApplyDetailService.selectApplySettShouldAmount(applySettShouldAmount);
            dynamicNorm.setSettWillPay(settShouldAmount);
            //实付工程款(元)
            Map<String, Object> payBillActPayAmountOne = Maps.newHashMap();
            payBillActPayAmountOne.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountOne.put("endPayDate",  ParserUtil.currentMonth());
            payBillActPayAmountOne.put("proCode", dynamicNorm.getProCode());
            BigDecimal payBillActPayAmountOnes = payBillDetailService.selectPayBillActPayAmountOne(payBillActPayAmountOne);
            dynamicNorm.setActProPrice(payBillActPayAmountOnes);
            //应付代付费(元)
            Map<String, Object> applyRepalceShouldAmount = Maps.newHashMap();
            applyRepalceShouldAmount.put("sureMonth", dynamicNorm.getStsMonth());
            applyRepalceShouldAmount.put("proCode", dynamicNorm.getProCode());
            BigDecimal repalceShouldAmount = bizcostApplyDetailService.selectApplyRepalceShouldAmount(applyRepalceShouldAmount);
            dynamicNorm.setReplaceWillPay(repalceShouldAmount);
            //已付代付费(元)
            Map<String, Object> payBillActPayAmountTwo = Maps.newHashMap();
            payBillActPayAmountTwo.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountTwo.put("endPayDate",  ParserUtil.currentMonth());
            payBillActPayAmountTwo.put("proCode", dynamicNorm.getProCode());
            BigDecimal payBillActPayAmountTwos = payBillDetailService.selectPayBillActPayAmountTwo(payBillActPayAmountTwo);
            dynamicNorm.setActReplacePay(payBillActPayAmountTwos);
            //已支付建设期利息(元)
            Map<String, Object> payBillActPayAmountThree = Maps.newHashMap();
            payBillActPayAmountThree.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountThree.put("endPayDate",  ParserUtil.currentMonth());
            payBillActPayAmountThree.put("proCode", dynamicNorm.getProCode());
            BigDecimal payBillActPayAmountThrees = payBillDetailService.selectPayBillActPayAmountThree(payBillActPayAmountThree);
            dynamicNorm.setActBuildIns(payBillActPayAmountThrees);
            //数据状态(默认为有效)
            dynamicNorm.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            dynamicNorm.setAddBy("system");
            dynamicNorm.setAddTime(new Date());
            dynamicNorm.setUptBy("system");
            dynamicNorm.setUptTime(new Date());
            dynamicNormService.insert(dynamicNorm);
        }
    }
}